/*
*  Impp - Instant Messenger
*
*  Copyright (C) 2008  Oliver Schneider
*
*  This program is free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  This program is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
#ifndef PACKAGEUTILS_H_
#define PACKAGEUTILS_H_

#include <iostream>
#include <vector>
#include <wx/string.h>
#include <wx/longlong.h>

#include "../../core/Status.h"
#include "PackageConstants.h"

namespace Impp {

typedef unsigned char 		byte; 		//1 byte
typedef unsigned short 		word; 		//2 byte
typedef unsigned int		dword; 		//4 byte
typedef unsigned long long	qword; 		//8 byte

struct Header{
	byte 	id;
	byte 	channel;
	word 	sequenNrBE;
	word 	sizeBE;
};

struct Tlv{
	word 	type;
	word 	length;
	byte* 	data;
};

struct Snac{
	word 	family;
	word 	subtype;
	word 	flags;
	dword 	requestId;
};

struct Rate {
	word 	id;
	dword	windowSize;
	dword 	clearLevel, alertLevel, limitLevel, disconnectLevel, currentLevel, maxLevel;
	dword 	lastTime;
	byte 	currentState;
	wxLongLong timeCurrentLevel;
};

struct RateGroup {
	word 				id;
	std::vector<dword> 	familyPairs;
};

struct Item{
	wxString itemName;
	word groupId;
	word itemId;
	word typeOfItems;
	std::vector<Tlv> data;
};



struct ICQGroup{
	wxString screenName;
	dword id;
	dword subgroup;
	size_t itemId;
};


struct ICQBuddy{
	wxString screenName;
	wxString customName;
	dword id;
	dword subgroup;
	size_t itemId;
};

struct AvatarInfo {
	AvatarInfo():
		id(0),
		flags(0),
		hasLng(0)
	{}
	 word id;
	 byte flags;
	 byte hasLng;
	 byte hash[16];
};

struct UserInfo {
	
	UserInfo() :
		uin(wxEmptyString),
		userClass(0),
		ip(0),
		status(ICQ_STATUS_OFFLINE),
		onlineTime(0),
		idleTime(0),
		onlineTimeData(0),
		accountData(0),
		avatar(),
		newCaps(false),
		newIdleTime(false)
	{}
	
	wxString uin;
	word userClass;
	dword ip;
	ICQStatus status;
	std::vector< std::vector<byte> > capabilities;
	qword onlineTime;
	qword idleTime;
	qword onlineTimeData;
	qword accountData;
	AvatarInfo avatar;
	int icqmood;
	bool newCaps;
	bool newIdleTime;
};

union Ip{
	Ip(dword i) : full(i){};
	dword full;
	byte part[4];
};

struct IpAndPort{
	wxString ip;
	long int port;
	
	IpAndPort(wxString serverAdressString);
};

struct FileTransfareInfo{
	dword proxyIP;
	dword ip;
	dword externIP;
	word ipPort;
};

class LoginCookie{

public:
	byte cookie[512];
	size_t lng;
	
public:
	LoginCookie() : lng(0) {};
	
	void setData(byte* cookieData, size_t lng){
		if(lng <= 512){
			memcpy(this->cookie,cookieData,lng);
			this->lng = lng;
		}else{
			wxLogError(wxT("Can not connect to Server, Cookie was too large!"));
		}
	}
	
};



word toWord(byte* b2);
void copy(byte* src, byte* dest, size_t lng);
word swapBytes(word w);
wxString byteToWxString(byte* data, size_t lng);

void toHex(std::ostream & out, byte* data, size_t lng);
void toHex(std::ostream & out, byte & b);
void toHex(std::ostream & out, word & w);
void removeHtml(wxString & s);

bool capEquals(byte* c1, byte* c2);

void deleteAllocedTlv(Tlv* tlv);
void mewAllocedTlv(Tlv* tlv, const word & type, const byte* newValue, const word & newSize);
void toAllocedTlv(Tlv* tlv);

ICQStatus toIcqStatus(Status status);
Status icqStatusToStatus(ICQStatus status);

wxString toHexString(byte* data, size_t lng);
wxString ipToString(const dword & ip);

}

#endif /*PACKAGEUTILS_H_*/
